import Axios from "axios";
import { BaseService } from "./BaseService";

export class ComponentService extends BaseService {
    public static readonly instance = new ComponentService();

    public async fetchData(apiConfig: { url: string; method: string; params?: any }): Promise<any> {
        try {
            console.log(apiConfig);
            switch (apiConfig.method) {
                case 'GET':
                    return (await Axios.get(`/api${apiConfig.url}`, {params: apiConfig.params}));
                case 'POST':
                    return (await Axios.post(`/api${apiConfig.url}`, apiConfig.params));
                case 'PUT':
                    return (await Axios.put(`/api${apiConfig.url}`, apiConfig.params));
                case 'DELETE':
                    return (await Axios.delete(`/api${apiConfig.url}`));
                default:
                    throw new Error(`Unsupported method: ${apiConfig.method}`);
            }
        } catch (error) {
            console.error(`Failed to load data from ${apiConfig.url}:`, error);
            throw error;
        }

        // try {
        //   const { url, method, params } = apiConfig;

        //   let response;
        //   if (method.toUpperCase() === "GET") {
        //     response = await Axios.get(`/api${url}`, { params });
        //   } else {
        //     response = await Axios.request({ url: '/api' + url, method, data: params });
        //   }

        //   return response.data;
        // } catch (error) {
        //   console.error(`Failed to load data from ${apiConfig.url}:`, error);
        //   throw error;
        // }
    }
}